package org.come.Jpanel;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

import org.come.Frame.TaobaoCourtMainJframe;
import org.come.model.Eshop;
import org.come.until.CutButtonImage;
import org.come.until.GoodsListFromServerUntil;

import com.tool.btn.CommonBtn;
import com.tool.btn.FormsOnOffBtn;
import com.tool.btn.TextBtn;
import com.tool.role.RoleData;
import com.tool.tcpimg.UIUtils;
import com.updateNew.MyIsif;

/**
 * 物品购买
 * 
 * @author zeng
 * @time 2019-5-8
 */
public class GoodDetailedJpanel extends JPanel {

	// 充值
	private TextBtn changeXY;
	// 购买按钮 , 数量增加 , 数量减少
	private CommonBtn buyBtn, addBtn, reduceBtn;
	// 物品单价 , 消耗仙玉 , 拥有仙玉
	private JLabel wupinPrice, xiaohaoXianyu, yonghuXianyu;
	// 购买数量
	private JTextField goumaiCount;
	// 售价 , 售价数量 , 含有多彩仙玉 , 多彩仙玉值
	private JLabel priceLa, priceVal, haveDuoCai, duoCaiVal;
	// 物品详情
	private JLabel wupinDetail;

	private JLabel goodName;
	// private final String text1 =
	// "<html><body><font color='#FFFF00'>你确定要购买</font><font color='#00FF00'>";
	// private final String text2 =
	// "</font><font color='#FFFF00'>吗?</font></body></html>";
	private Eshop eshop; // 选中的物品
	private String nowMoneyType; // 当前消耗类型

	// private JScrollPane jScrollPane1,// 放置法术列表的滚动条
	// jScrollPane2;// 放置法术说明的滚动条

	public GoodDetailedJpanel() throws Exception {
			this.setPreferredSize(new Dimension(426, 462));
			this.setLayout(null);
			this.setOpaque(false);

			// 关闭按钮事件
			FormsOnOffBtn offBtn = new FormsOnOffBtn("inkImg/number/b8/off.png", 1, 44);
			offBtn.setBounds(395, 5, 25, 25);
			this.add(offBtn);

			// 内容显示
			// 物品单价
			wupinPrice = new JLabel();
			wupinPrice.setFont(UIUtils.TEXT_FONT1);
			wupinPrice.setText("3");
			wupinPrice.setOpaque(false);
			wupinPrice.setForeground(Color.WHITE);
			wupinPrice.setBounds(0, 0, 0, 0);
			// 购买数量
			goumaiCount = new JTextField();
			goumaiCount.setFont(UIUtils.TEXT_FONT1);
			goumaiCount.setText("0");
			goumaiCount.setForeground(Color.WHITE);
			goumaiCount.setOpaque(false);
			goumaiCount.setBorder(null);
			goumaiCount.setBounds(275, 319, 164 - 26, 16);
			goumaiCount.getDocument().addDocumentListener(new DocumentListener() {

				@Override
				public void removeUpdate(DocumentEvent e) {
					// TODO Auto-generated method stub
					if ("".equals(goumaiCount.getText().trim())) {
						xiaohaoXianyu.setText("0");

						priceVal.setText("0" + nowMoneyType);
					} else {
						int wupinCount = Integer.valueOf(goumaiCount.getText());
						int goodsPrice = Integer.valueOf(wupinPrice.getText());
						xiaohaoXianyu.setText((goodsPrice * wupinCount) + "");
						priceVal.setText((goodsPrice * wupinCount) + nowMoneyType);
					}

				}

				@Override
				public void insertUpdate(DocumentEvent e) {
					int wupinCount = Integer.valueOf(goumaiCount.getText());
					int goodsPrice = Integer.valueOf(wupinPrice.getText());
					xiaohaoXianyu.setText((goodsPrice * wupinCount) + "");
					priceVal.setText((goodsPrice * wupinCount) + nowMoneyType);
				}

				@Override
				public void changedUpdate(DocumentEvent e) {
				}
			});

			// 限制输入内容为数字切数字不大于100
			goumaiCount.addKeyListener(new KeyListener() {
				@Override
				public void keyTyped(KeyEvent e) {
					// TODO Auto-generated method stub
					int charstr = e.getKeyChar();
					String str = goumaiCount.getText();
					if (str.length() == 0) {
						goumaiCount.setText(0 + "");
					}

					// control the value between 0 and 9
					if (charstr < KeyEvent.VK_0 || charstr > KeyEvent.VK_9) {
						e.consume();
					}
					if (str.length() == 1) {
						if ("0".equals(str)) {
							goumaiCount.setText("");
						}
					}
					if (str.length() > 0) {
						if (str.length() > 1) {
							if (str.length() > 2) {
								e.consume();
								goumaiCount.setText(999 + "");
							}
							// int sum = Integer.parseInt(goumaiCount.getText());
							// long price = Long.parseLong(Unitprice.toString());
							// labTotalsum.setText(price*sum+"");
							// Totalsum = new BigDecimal((price * sum));
						}
					}
				}

				@Override
				public void keyPressed(KeyEvent e) {
				}

				@Override
				public void keyReleased(KeyEvent e) {
				}

			});

			// 消耗仙玉
			xiaohaoXianyu = new JLabel();
			xiaohaoXianyu.setFont(UIUtils.TEXT_FONT1);
			xiaohaoXianyu.setText("0");
			xiaohaoXianyu.setForeground(Color.WHITE);
			xiaohaoXianyu.setOpaque(false);
			xiaohaoXianyu.setBounds(275, 349, 164 - 26, 16);
			// 拥有仙玉
			yonghuXianyu = new JLabel();
			yonghuXianyu.setFont(UIUtils.TEXT_FONT1);
			yonghuXianyu.setText("0");
			yonghuXianyu.setForeground(Color.WHITE);
			yonghuXianyu.setOpaque(false);
			yonghuXianyu.setBounds(275, 379, 164 - 26, 16);

			// 充值按钮 (TextBtn 中 caozuo == 1)
			changeXY = new TextBtn("inkImg/number/b8/chongzhi3.png", 1, "", 888);
			changeXY.setBounds(325 -75, 424, 71, 30);
			// 确定购买 (CommonBtn 中 caozuo == 1)
			buyBtn = new CommonBtn("inkImg/number/b8/btnbuy.png", 1, "", 1, this,null);
			buyBtn.setBounds(325, 424, 71, 30);
			// 添加 (CommonBtn 中 caozuo == 2)
			addBtn = new CommonBtn("inkImg/number/b8/czbtn.png", 1, "", 2, this);
			addBtn.setBounds(370, 317, 19, 20);
			// 减少 (CommonBtn 中 caozuo == 3)
			reduceBtn = new CommonBtn("inkImg/number/b8/reduceBtn.png", 1, "", 3, this);
			reduceBtn.setBounds(252, 317, 19, 20);

			// 售价:
			priceLa = new JLabel("");
			priceLa.setFont(UIUtils.TEXT_FONT1);
			priceLa.setForeground(Color.WHITE);
			priceLa.setBounds(143, 211, 45, 20);
			// 售价仙玉数
			priceVal = new JLabel();
			priceVal.setFont(UIUtils.TEXT_FONT1);
			priceVal.setText("1" + nowMoneyType);
			priceVal.setOpaque(false);
			priceVal.setForeground(new Color(3, 255, 10));
			priceVal.setBounds(230, 224, 120, 20);
			// 多彩仙玉
			// haveDuoCai = new JLabel("多彩仙玉");
			// haveDuoCai.setFont(UIUtils.TEXT_COM_FONT1);
			// haveDuoCai.setForeground(new Color(8, 4, 8));
			// haveDuoCai.setBounds(189, 211, 60, 20);

			// 物品详情
			wupinDetail = new JLabel();
			wupinDetail.setOpaque(false);
			wupinDetail.setIcon(CutButtonImage.getImage("img/item/1.png", 114, 114));
			wupinDetail.setBounds(145, 74, 114, 114);

			// 商品名
			goodName = new JLabel();
			goodName.setFont(UIUtils.TEXT_BOLD_FONT);
			// goodName.setForeground(Color.yellow);
			goodName.setBounds(55, 15, 300, 40);
			goodName.setOpaque(false);
			goodName.setHorizontalAlignment(SwingConstants.CENTER);
			this.add(goodName);

			this.add(wupinDetail);

			this.add(priceLa);
			this.add(priceVal);

			this.add(addBtn);
			this.add(reduceBtn);

			this.add(wupinPrice);
			this.add(goumaiCount);
			this.add(xiaohaoXianyu);
			this.add(yonghuXianyu);

//			this.add(changeXY);
			this.add(buyBtn);


	}

	public void gainGoodsMessage(Eshop eshop) {
			this.eshop = eshop;
			if (eshop == null) {
				return;
			}
			goodName.setText("<html><body><font color='#ffe182'>您确定要购买</font><font color='#ffe182'>" + eshop.getEshopname() + "</font><font color='#ffe182'>吗?</font></body></html>");
			wupinDetail.setIcon(GoodsListFromServerUntil.imgpathAdaptive(eshop.getEshopskin(), 114, 114));
			wupinPrice.setText(eshop.getEshopprice());
			if (TaobaoCourtMainJframe.getTaobaoCourtJframe().getTaobaoCourtMainJpanel().getTaobaoCourtCardJpanel().getShopOnlineJpanel().getShopType() == 11) {
				nowMoneyType = "积分";
				yonghuXianyu.setText(RoleData.getRoleData().getLoginResult().getMoney().toString());
			} else {
				yonghuXianyu.setText(RoleData.getRoleData().getLoginResult().getCodecard().toString());
				nowMoneyType = "仙玉";
			}
			goumaiCount.setText("1");

	}

	// 背景
	private ImageIcon icon;

	@Override
	protected void paintComponent(Graphics g) {
		super.paintComponent(g);
			// 技能
			if (icon == null) {
				icon = new ImageIcon("inkImg/number/b8/购买_w356,h4871.png");
			}
			g.drawImage(icon.getImage(), 0, 0, 426, 462, this);


	}

	public JLabel getWupinPrice() {
		return wupinPrice;
	}

	public void setWupinPrice(JLabel wupinPrice) {
		this.wupinPrice = wupinPrice;
	}

	public JTextField getGoumaiCount() {
		return goumaiCount;
	}

	public void setGoumaiCount(JTextField goumaiCount) {
		this.goumaiCount = goumaiCount;
	}

	public JLabel getXiaohaoXianyu() {
		return xiaohaoXianyu;
	}

	public void setXiaohaoXianyu(JLabel xiaohaoXianyu) {
		this.xiaohaoXianyu = xiaohaoXianyu;
	}

	public JLabel getYonghuXianyu() {
		return yonghuXianyu;
	}

	public void setYonghuXianyu(JLabel yonghuXianyu) {
		this.yonghuXianyu = yonghuXianyu;
	}

	public JLabel getPriceVal() {
		return priceVal;
	}

	public void setPriceVal(JLabel priceVal) {
		this.priceVal = priceVal;
	}

//	public TextBtn getChangeXY() {
//		return changeXY;
//	}
//
//	public void setChangeXY(TextBtn changeXY) {
//		this.changeXY = changeXY;
//	}

	public CommonBtn getBuyBtn() {
		return buyBtn;
	}

	public void setBuyBtn(CommonBtn buyBtn) {
		this.buyBtn = buyBtn;
	}

	public CommonBtn getAddBtn() {
		return addBtn;
	}

	public void setAddBtn(CommonBtn addBtn) {
		this.addBtn = addBtn;
	}

	public CommonBtn getReduceBtn() {
		return reduceBtn;
	}

	public void setReduceBtn(CommonBtn reduceBtn) {
		this.reduceBtn = reduceBtn;
	}

	public JLabel getPriceLa() {
		return priceLa;
	}

	public void setPriceLa(JLabel priceLa) {
		this.priceLa = priceLa;
	}

	public JLabel getHaveDuoCai() {
		return haveDuoCai;
	}

	public void setHaveDuoCai(JLabel haveDuoCai) {
		this.haveDuoCai = haveDuoCai;
	}

	public JLabel getDuoCaiVal() {
		return duoCaiVal;
	}

	public void setDuoCaiVal(JLabel duoCaiVal) {
		this.duoCaiVal = duoCaiVal;
	}

	public JLabel getWupinDetail() {
		return wupinDetail;
	}

	public void setWupinDetail(JLabel wupinDetail) {
		this.wupinDetail = wupinDetail;
	}

	public JLabel getGoodName() {
		return goodName;
	}

	public void setGoodName(JLabel goodName) {
		this.goodName = goodName;
	}

	public Eshop getEshop() {
		return eshop;
	}

	public void setEshop(Eshop eshop) {
		this.eshop = eshop;
	}

	public ImageIcon getIcon() {
		return icon;
	}

	public void setIcon(ImageIcon icon) {
		this.icon = icon;
	}

	public String getNowMoneyType() {
		return nowMoneyType;
	}

	public void setNowMoneyType(String nowMoneyType) {
		this.nowMoneyType = nowMoneyType;
	}

}
